package com.fbs.job.admin.controller;

import com.fbs.job.admin.core.entity.FbsJobUser;
import com.fbs.job.admin.core.util.CookieUtil;
import com.fbs.job.admin.core.util.DateTool;
import com.fbs.job.admin.service.JobUserService;
import com.fbs.job.core.model.ResultT;
import com.fbs.job.core.util.JsonTool;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.*;


@Controller
public class IndexController {
    @Autowired
    private JobUserService jobUserService;


    @RequestMapping("/")
    public String index(Model model,HttpServletRequest request,HttpServletResponse response) {
        FbsJobUser fbsJobUser = jobUserService.ifLogin(request, response);
        model.addAttribute("account",fbsJobUser.getUsername());
        return "index";
    }

    @RequestMapping("/teach")
    public String teach(Model model) {
        return "teach/teach";
    }

    @RequestMapping("/home")
    public String home(Model model) {

        Map<String, Object> dashboardMap = jobUserService.dashboardInfo();
        model.addAllAttributes(dashboardMap);

        return "home";
    }


    @PostMapping("/chartInfo")
    @ResponseBody
    public ResultT<Map<String, Object>> chartInfo(String startDate, String endDate)  {

        Date dateFrom = DateTool.toYMDHMSDate(startDate);
        Date dateTo = DateTool.toYMDHMSDate(endDate);

        ResultT<Map<String, Object>> chartInfo = jobUserService.chartInfo(dateFrom, dateTo);
        return chartInfo;
    }


    @RequestMapping("/toLogin")
    public String toLogin(Model model) {
        return "login";
    }



    @ResponseBody
    @RequestMapping("/login")
    public ResultT login(HttpServletRequest request, HttpServletResponse response, @RequestBody FbsJobUser fbsJobUser ) {
        return jobUserService.login(request,response,fbsJobUser.getUsername(),fbsJobUser.getPassword(), StringUtils.isBlank(fbsJobUser.getIfRemember())?false:true);
    }

    @ResponseBody
    @RequestMapping("/logout")
    public ResultT<String> logout(HttpServletRequest request, HttpServletResponse response){
        return jobUserService.logout(request,response);
    }


    @ResponseBody
    @GetMapping("/menu")
    public Map<String,Object> menu(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> homeInfo = new TreeMap<>();
        homeInfo.put("title","任务调度中心");
        homeInfo.put("href","home");
        Map<String,Object> logoInfo = new TreeMap<>();
        logoInfo.put("title","任务调度中心");
        logoInfo.put("image","images/logo.png");
        logoInfo.put("href","home");
        logoInfo.put("target","_self");
        Map<String,Object> menuInfo = new TreeMap<>();
        menuInfo.put("title","");
        menuInfo.put("href","");
        menuInfo.put("target","_self");
        List child = new ArrayList();
        menuInfo.put("child",child);


        FbsJobUser loginUser = jobUserService.ifLogin(request, response);
        boolean isAdmin= true;
        if (loginUser.getRole()!=1) {
            isAdmin= false;
        }
        child.add(JsonTool.fromJson("{\n" +
                "          \"title\": \"运行报表\",\n" +
                "          \"href\": \"home\",\n" +
                "          \"icon\": \"fa fa-dot-circle-o\",\n" +
                "          \"target\": \"_self\"\n" +
                "        }",Map.class));
        if (isAdmin){
            child.add(JsonTool.fromJson("{\n" +
                    "          \"title\": \"执行器管理\",\n" +
                    "          \"href\": \"job_actuator\",\n" +
                    "          \"icon\": \"fa fa-circle-o\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        }",Map.class));
        }


        child.add(JsonTool.fromJson("{\n" +
                "          \"title\": \"任务管理\",\n" +
                "          \"href\": \"job_info\",\n" +
                "          \"icon\": \"fa fa-dot-circle-o\",\n" +
                "          \"target\": \"_self\"\n" +
                "        }",Map.class));

        child.add(JsonTool.fromJson("{\n" +
                "          \"title\": \"调度日志\",\n" +
                "          \"href\": \"log_info\",\n" +
                "          \"icon\": \"fa fa-circle-o\",\n" +
                "          \"target\": \"_self\"\n" +
                "        }",Map.class));

        if (isAdmin){
            child.add(JsonTool.fromJson("{\n" +
                    "          \"title\": \"用户管理\",\n" +
                    "          \"href\": \"user\",\n" +
                    "          \"icon\": \"fa fa-dot-circle-o\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        }",Map.class));
        }

        child.add(JsonTool.fromJson("{\n" +
                "          \"title\": \"使用教程\",\n" +
                "          \"href\": \"teach\",\n" +
                "          \"icon\": \"fa fa-circle-o\",\n" +
                "          \"target\": \"_self\"\n" +
                "        }",Map.class));


        Map<String,Object> mapRT = new TreeMap<>();
        mapRT.put("homeInfo",homeInfo);
        mapRT.put("logoInfo",logoInfo);
        List list = new ArrayList();
        list.add(menuInfo);
        mapRT.put("menuInfo",list);
        return mapRT;
    }
}
